<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-12-22 10:18:19
 * @Description: 订单绩效
 */
class ScsOrderPerformanceController extends BaseController
{
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-22 09:50:42
     * @Description: 搜索订单
     */
    public  function search_order(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $me=$this->verificationMaintenanceEngineerToken(Input::get("token"),$response);
            if(is_null($me)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $orders=Orders::where("shop_id",$me->type_id)->whereIn('status',Orders::$prer_status)->orderBy("book_time",'desc');
            if(Input::get("order_id")){
                $orders=$orders->where("order_id","like","%".Input::get("order_id")."%");
            }
            if(Input::get("car_no")){
                $orders=$orders->where("car_no",'like','%'.Input::get("car_no").'%');
            }
            //if(Input::get("phone")){
            //    $orders=$orders->where("phone",'like','%'.Input::get("phone").'%');
            //}
            if(Input::get("book_time")){
                $orders=$orders->where("book_time",Input::get("book_time"));
            }
            if(Input::get("place")){
                $orders=$orders->where("place",Input::get("place"));
            }
            if(Input::get("order_type")){
                $orders=$orders->where("order_type",Input::get("order_type"));
            }
            //过滤钣金类型的车
            $t_orders=clone $orders;
            $temp_order=$t_orders->select("order_id")->take(1000)->get();
            $fillt_arr=OrderPerformance::checkOrder($temp_order);
            $orders=$orders->whereIn("order_id",$fillt_arr);

            $count=$orders->count();
            $orders=$orders->skip($page_size*($page-1))->take($page_size)->get();
            $arr_order=array();
            foreach ($orders as $_order) {
                $op=OrderPerformance::where('order_id',$_order->order_id)->first();
                if(is_null($op)){
                    $time='';
                }else{
                    $time=$op->created_at->toDateTimeString();
                }
                $arr_order[]=array(
                    "order_id"=>$_order->order_id,
                    "pay_time"=>$_order->pay_time,
                    "start_time"=>$_order->start_time,
                    "end_time"=>$_order->end_time,
                    "book_time"=>$_order->book_time,
                    "book_hour"=>$_order->book_hour,
                    "place"=>$_order->place,
                    "order_type"=>$_order->order_type,
                    "status"=>$_order->status,
                    "created_at"=>$_order->created_at."",
                    "car_no"=>$_order->car_no,
                    //"phone"=>$_order->phone,
                    "brand_name"=>$_order->car_brand_name,
                    "series_name"=>$_order->car_series_name,
                    "model_name"=>$_order->car_model_name,
                    //"mileage"=>$_order->mileage
                    "performance_time"=>$time
                );
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"orders"=>$arr_order));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "search_order", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      lyt
     * @DateTime:    2017-02-24 14:59:35
     * @Description: 检查订单是否可以修改绩效
     */
    public  function check_order(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $me=$this->verificationMaintenanceEngineerToken(Input::get("token"),$response);
            if(is_null($me)){
                break;
            }
            $op=OrderPerformance::where('order_id',Input::get("order_id"))->first();
            if($op){//如果存在检测记录
                if(date("Y-m",strtotime($op->created_at))!=date("Y-m",time())){
                    $response = Lang::get("scs_error.order_checek_time_error_40078");
                    return $response;
                }
            }
            return array('code' => 0, 'msg' => 'ok', 'result' => array());
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "check_order", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-22 14:03:35
     * @Description: 创建订单绩效
     */
    public  function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                "items"=>'required',//[{"order_product_id":[1,2,3],"process_id":2,"team_id":2},{"order_product_id":[1,2,3],"process_id":2,"team_id":2}]
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $me=$this->verificationMaintenanceEngineerToken(Input::get("token"),$response);
            if(is_null($me)){
                break;
            }
            $order_id=Input::get('order_id');
            $items=json_decode(Input::get('items'),1);
            $op=OrderPerformance::where('order_id',$order_id)->first();
            if($op){//如果存在检测记录
                if(date("Y-m",strtotime($op->created_at))!=date("Y-m",time())){
                    $response = Lang::get("scs_error.order_checek_time_error_40078");
                    return $response;
                }
            }            
            $response=DB::transaction(function () use ($items,$order_id,$me) {
                $ops=OrderPerformance::where('order_id',$order_id)->get();
                if(!is_null($ops)){
                    OrderPerformance::where('order_id',$order_id)->delete();
                    OrderPerformanceEmployee::where('order_id',$order_id)->delete();
                }
                foreach($items as $item ){
                    $team=Teams::find($item['team_id']);
                    if($team->status != 1){
                        $response = Lang::get("scs_error.team_in_used_error_90016");
                        return $response;
                    
                    }
                    if($team->type_id != $item['process_id']){
                        $response = Lang::get("scs_error.team_in_used_error_90015");
                        return $response;
                        
                    }
                    $op=new OrderPerformance;
                    $op->order_id=$order_id;
                    $op->process_id=$item['process_id'];
                    $op->order_product_id=json_encode($item['order_product_id']);
                    $op->team_id=$item['team_id'];
                    $op->save();
                }
                foreach($items as $item){
                    $mes=MaintenanceEngineers::where('type','shop')->where('type_id',$me->type_id)->where('status',1)->where('team',$item['team_id'])->select('id')->get();
                    $sc=SCUser::where('shop_id',$me->type_id)->where('status',1)->where('team',$item['team_id'])->select('id')->get();
                    foreach($mes as $_me){
                        $ope=new OrderPerformanceEmployee;
                        $ope->order_id=$order_id;
                        $ope->process_id=$item['process_id'];
                        $ope->user_type=2;
                        $ope->user_id=$_me->id;
                        $ope->save();
                    }
                    foreach($sc as $_sc){
                        $ope=new OrderPerformanceEmployee;
                        $ope->order_id=$order_id;
                        $ope->process_id=$item['process_id'];
                        $ope->user_type=1;
                        $ope->user_id=$_sc->id;
                        $ope->save();
                    }
                }
                return array('code' => 0, 'msg' => 'ok', 'result' => array());
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-12-22 09:50:42
     * @Description: 订单绩效详情
     */
    public  function detail(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "order_id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $me=$this->verificationMaintenanceEngineerToken(Input::get("token"),$response);
            if(is_null($me)){
                break;
            }
            $tt=TeamType::all();
            foreach($tt as $_tt){
                $teams=Teams::where('status',1)->where('type_id',$_tt->id)->select('id','name')->get();
                $_tt->teams=$teams;
            }
            $order=Orders::find(Input::get('order_id'));
            $order_product=OrderProduct::where('order_id',Input::get('order_id'))->where('status',1)->select('id','order_id','product_name')->get();
            $op=OrderPerformance::where('order_id',Input::get('order_id'))->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>
                array("model"=>$order->car_model_name,"car_brand_id"=>$order->car_brand_id,"car_brand_name"=>$order->car_brand_name,
                    "car_series_id"=>$order->car_series_id,"car_series_name"=>$order->car_series_name,
                    "car_model_id"=>$order->car_model_id,"car_model_name"=>$order->car_model_name,
                    "order_id"=>$order->order_id,"car_no"=>$order->car_no, "order_product"=>$order_product,'order_performance'=>$op,'team_type'=>$tt));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "search_order", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}